import PreviewVideo from "../src/PreviewVideo.vue"
import {createApp, ref,h} from "vue";


export type UserPreviewVideoReturnType = [ReturnMethods];


interface ReturnMethods {
    /**
     * 显示视频
     * @param urls
     * @constructor
     */
    OpenUrl(url: string ): void
}

/**
 * 使用图片预览
 */
export function usePreviewVideo(): UserPreviewVideoReturnType {
    //打开
    const openUrl = async (url: string | string[]) => {
        const box = document.createElement('div')
        const app = ref();
        app.value = createApp(h(PreviewVideo, {
            onRegister: (openMethod: any) => {
                openMethod.closeCallback = () => {
                    app.value.unmount();
                    app.value = null;
                    document.body.removeChild(box);
                };
                openMethod.openUrl(url);
            }
        }))
        document.body.appendChild(box);
        app.value.mount(box);
    };
    const methods: ReturnMethods = {
        OpenUrl(url: string | string[]): void {
            openUrl(url);
        }
    };
    return [methods];
}

